<?php

namespace app\controller;

use app\BaseController;
use app\service\DriverService;
use think\exception\ValidateException;
use think\facade\Request;

/**
 * 司机
 */
class Driver extends BaseController
{
    /**
     * @desc 司机列表
     * @return void
     */
    public function lists()
    {
        $uid = Request::param('uid/d', 0);
        $param['is_export'] = Request::param('is_export/d', 0);
        $param['uid'] = $uid;
        $param['partner_id'] = get_partner_id($uid);
        $param['name'] = Request::param('name/s', '');
        $param['mobile'] = Request::param('mobile/s', '');
        $param['company_name'] = Request::param('company_name/s', '');
        $param['page'] = Request::param('page/d', 0);
        $param['limit'] = Request::param('limit/d', 10);
        $info = DriverService::lists($param);
        returnSuccess(200, '获取成功', $info);
    }

    /**
     * @desc 司机考勤列表
     * @return void
     */
    public function attendance_lists()
    {
        $uid = Request::param('uid/d', 0);
        $param['partner_id'] = get_partner_id($uid);
        $param['date'] = Request::param('date/s', '');
        //        $param['name']     = Request::param('name/s', '');
        $param['page'] = Request::param('page/d', 0);
        $param['limit'] = Request::param('limit/d', 10);
        $info = DriverService::attendance_lists($param);
        returnSuccess(200, '获取成功', $info);
    }

    //新增司机信息
    public function add()
    {
        $uid = Request::param('uid/d', 0);
        $logParam['uid'] = $uid;
        $param['partner_id'] = get_partner_id($uid);
        $param['name'] = Request::param('name/s', '');
        $param['mobile'] = Request::param('mobile/s', '');
        $param['age'] = Request::param('age/d', 0);
        $param['card_no'] = Request::param('card_no/s', '');
        $param['card_address'] = Request::param('card_address/d', '');
        $param['card_province_id'] = Request::param('card_province_id/d', 0);
        $param['card_city_id'] = Request::param('card_city_id/d', 0);
        $param['card_district_id'] = Request::param('card_district_id/d', 0);
        $param['residence_province_id'] = Request::param('residence_province_id/d', 0);
        $param['residence_city_id'] = Request::param('residence_city_id/d', 0);
        $param['residence_district_id'] = Request::param('residence_district_id/d', 0);
        $param['residence_address'] = Request::param('residence_address/s', '');
        $param['login_pass'] = Request::param('login_pass/d', 0);
        $param['work_type'] = Request::param('work_type/d', 1);
        $param['license_type_id'] = Request::param('license_type_id/d', 0);
        $param['license_pic1'] = Request::param('license_pic1/s', 0);
        $param['license_pic2'] = Request::param('license_pic2/s', 0);
        $param['sex'] = Request::param('sex/d', 1);
        $param['status'] = Request::param('status/d', 1);
        $param['is_format'] = Request::param('is_format/d', 0);
        $param['format_time'] = Request::param('format_time/s', "0000-00-00 00:00:00");
        $param['entry_time'] = Request::param('entry_time/s', "0000-00-00 00:00:00");
        $param['leave_time'] = Request::param('leave_time/s', "0000-00-00 00:00:00");
        $param['company_id'] = Request::param('company_id/d', 0);
        $param['department_id'] = Request::param('department_id/d', 0);
        $logParam['ip'] = Request::ip();
        try {
            validate(\app\validate\DriverValidate::class)->check([
                'name' => $param['name'],
                'mobile' => $param['mobile'],
                'age' => $param['age'],
                'card_no' => $param['card_no'],
                'card_province_id' => $param['card_province_id'],
                'card_city_id' => $param['card_city_id'],
                'card_district_id' => $param['card_district_id'],
                'residence_province_id' => $param['residence_province_id'],
                'residence_city_id' => $param['residence_city_id'],
                'residence_district_id' => $param['residence_district_id'],
                'residence_address' => $param['residence_address'],
                'login_pass' => $param['login_pass'],
                'work_type' => $param['work_type'],
                'license_type_id' => $param['license_type_id'],
                'license_pic1' => $param['license_pic1'],
                'license_pic2' => $param['license_pic2'],
            ]);
        } catch (ValidateException $e) {
            returnFail(21009, $e->getError());
        }
        $data = DriverService::add($param, $logParam);
        if ($data == 10000) {
            returnSuccess(200, '添加成功');
        } else {
            returnFail(21101, '添加失败');
        }
    }

    //修改司机信息
    public function modify()
    {
        $uid = Request::param('uid/d', 0);
        $id = Request::param('id/d', 0);
        $logParam['uid'] = $uid;
        $param['partner_id'] = get_partner_id($uid);
        $param['name'] = Request::param('name/s', '');
        $param['mobile'] = Request::param('mobile/s', '');
        $param['age'] = Request::param('age/d', 0);
        $param['card_no'] = Request::param('card_no/s', '');
        $param['card_address'] = Request::param('card_address/d', '');
        $param['card_province_id'] = Request::param('card_province_id/d', 0);
        $param['card_city_id'] = Request::param('card_city_id/d', 0);
        $param['card_district_id'] = Request::param('card_district_id/d', 0);
        $param['residence_province_id'] = Request::param('residence_province_id/d', 0);
        $param['residence_city_id'] = Request::param('residence_city_id/d', 0);
        $param['residence_district_id'] = Request::param('residence_district_id/d', 0);
        $param['residence_address'] = Request::param('residence_address/s', '');
        $param['login_pass'] = Request::param('login_pass/d', 0);
        $param['work_type'] = Request::param('work_type/d', 1);
        $param['license_type_id'] = Request::param('license_type_id/d', 0);
        $param['license_pic1'] = Request::param('license_pic1/s', 0);
        $param['license_pic2'] = Request::param('license_pic2/s', 0);
        $param['sex'] = Request::param('sex/d', 1);
        $param['status'] = Request::param('status/d', 1);
        $param['is_format'] = Request::param('is_format/d', 0);
        $param['format_time'] = Request::param('format_time/s', "0000-00-00 00:00:00");
        $param['entry_time'] = Request::param('entry_time/s', "0000-00-00 00:00:00");
        $param['leave_time'] = Request::param('leave_time/s', "0000-00-00 00:00:00");
        $param['company_id'] = Request::param('company_id/d', 0);
        $param['department_id'] = Request::param('department_id/d', 0);
        $logParam['ip'] = Request::ip();
        try {
            validate(\app\validate\DriverValidate::class)->check([
                'name' => $param['name'],
                'mobile' => $param['mobile'],
                'age' => $param['age'],
                'card_no' => $param['card_no'],
                'card_province_id' => $param['card_province_id'],
                'card_city_id' => $param['card_city_id'],
                'card_district_id' => $param['card_district_id'],
                'residence_province_id' => $param['residence_province_id'],
                'residence_city_id' => $param['residence_city_id'],
                'residence_district_id' => $param['residence_district_id'],
                'residence_address' => $param['residence_address'],
                'login_pass' => $param['login_pass'],
                'work_type' => $param['work_type'],
                'license_type_id' => $param['license_type_id'],
                'license_pic1' => $param['license_pic1'],
                'license_pic2' => $param['license_pic2'],
            ]);
        } catch (ValidateException $e) {
            returnFail(21009, $e->getError());
        }
        $data = DriverService::edit($param, $logParam, $id);
        if ($data == 10000) {
            returnSuccess(200, '修改成功');
        } else {
            returnFail(21101, '修改失败');
        }
    }

    //删除司机信息
    public function del()
    {
        $uid = Request::param('uid/d', 0);
        $logParam['uid'] = $uid;
        $param['partner_id'] = get_partner_id($uid);
        $id = Request::param('id/d', 0);
        $logParam['ip'] = Request::ip();
        $data = DriverService::del($param, $logParam, $id);
        if ($data == 10000) {
            returnSuccess(200, '删除成功', $data);
        } else {
            returnFail(21101, '删除失败');
        }
    }

    //工作记录
    public function workRecode()
    {
        $uid = Request::param('uid/d', 0);
        $param['is_export'] = Request::param('is_export');
        $param['stime'] = Request::param('stime/s', "");
        $param['etime'] = Request::param('etime/s', "");
        $param['uid'] = $uid;
        $param['partner_id'] = get_partner_id($uid);
        $param['driver_id'] = Request::param('driver_id/d', 0);
        $param['name'] = Request::param('name/s', '');
        $param['page'] = Request::param('page/d', 0);
        $param['limit'] = Request::param('limit/d', 10);
        $info = DriverService::workRecode($param);
        returnSuccess(200, '获取成功', $info);
    }

    public function income_lists()
    {
        $uid = Request::param('uid/d', 0);
        $param['is_export'] = Request::param('is_export');
        $param['stime'] = Request::param('stime/s', "");
        $param['etime'] = Request::param('etime/s', "");
        $param['uid'] = $uid;
        $param['partner_id'] = get_partner_id($uid);
        $param['driver_id'] = Request::param('driver_id/d', 0);
        $param['page'] = Request::param('page/d', 0);
        $param['limit'] = Request::param('limit/d', 10);
        $info = DriverService::income_lists($param);
        returnSuccess(200, '获取成功', $info);
    }
}
